package krow.dev.addetector.view.dialog;

import krow.dev.addetector.R;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ModuleScanDialog extends Dialog {

	private Context mContext;

	private TextView mTitleView;
	private TextView mIndexView;
	private TextView mSizeView;
	private TextView mPercentView;
	
	private ProgressBar mDownloadProgress;
	
	public ModuleScanDialog(Context context) {
		super(context);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		WindowManager.LayoutParams windowParams = getWindow().getAttributes();
		windowParams.dimAmount = 0.7f;
		getWindow().setAttributes(windowParams);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		setContentView(R.layout.dialog_scan);

		mContext = context;
		mTitleView = (TextView) findViewById(R.id.text_title);
		mIndexView = (TextView) findViewById(R.id.text_index);
		mSizeView = (TextView) findViewById(R.id.text_size);
		mPercentView = (TextView) findViewById(R.id.text_percent);
		mPercentView.setText("0%");
		
		mDownloadProgress = (ProgressBar) findViewById(R.id.progress_download);
		mDownloadProgress.setIndeterminate(false);
		mDownloadProgress.setMax(100);
	}

	@Override
	public void dismiss() {
		super.dismiss();
	}

	public void setTitle(String title) {
		mTitleView.setText(title);
	}

	public void setTitle(int resourceId) {
		mTitleView.setText(mContext.getString(resourceId));
	}
	
	public void setIndex(long index, long size) {
		if(size > 0){
			mIndexView.setText("(" + index + "/" + size + ")");
			mIndexView.setVisibility(View.VISIBLE);
		}else{
			mIndexView.setVisibility(View.GONE);
		}
	}
	
	public void setProgress(int index, int size){
		int progress = index * 100 / size;
		
		mSizeView.setText(String.format("%d/%d", index, size));
		mDownloadProgress.setProgress(progress);
		mPercentView.setText(String.valueOf(progress) + "%");
	}
}
